<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 组织架构内容的头部 -->
      <el-card class="tree-card">
        <tree-tools :tree-node="company" :is-root="true" @addDepts="addDepts" />
        <!-- 放置一个el-tree组件 -->
        <el-tree
          :data="departs"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <tree-tools
            slot-scope="{ data }"
            :tree-node="data"
            @delDepts="getDepartments"
            @addDepts="addDepts"
            @editDepts="editDepts"
          />
        </el-tree>
      </el-card>
    </div>
    <!-- 放置弹层组件 此处treeNode和tree-tools中的treeNode无关-->
    <add-dept
      ref="addDept"
      :showDialog.sync="showDialog"
      :treeNode="node"
      @addDepts="getDepartments"
    />
  </div>
</template>

<script>
import TreeTools from "./components/tree-tools";
import { getDepartments, getDepartmentDetail } from "@/api/departments";
import { transListToTreData } from "@/utils";
import AddDept from "./components/add-dept.vue";
export default {
  components: {
    TreeTools,
    AddDept,
  },
  data() {
    return {
      company: { name: "江苏未来信息科技有限公司", manager: "负责人", id: "" },
      departs: [],
      defaultProps: {
        label: "name",
        children: "children",
      },
      showDialog: false,
      node: null, //记录当前点击的node节点
    };
  },

  created() {
    this.getDepartments();
  },
  methods: {
    async getDepartments() {
      const data = await getDepartments();
      this.departs = transListToTreData(data.depts, ""); //需要将data.depts数组数据转换成树形结构
    },
    addDepts(node) {
      //这里的Node是tree-tools组件传过来的数据
      this.showDialog = true;
      this.node = node;
    },
    editDepts(node) {
      this.showDialog = true;
      this.node = node;
      this.$refs.addDept.getDepartmentDetail(node.id);
    },
  },
};
</script>

<style scope>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
